package com.parktech.sun.web.controller.management.industryservice;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.industryservice.IndustryService;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.industryservice.IndustryServiceService;
import com.parktech.sun.utils.FileHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * User: Think
 * Date: 11-1-12
 * Time: 下午10:52
 */
@Controller
@RequestMapping("/management/industryservices")
public class IndustryServiceController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private IndustryServiceService industryServiceService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public IndustryServiceController(IndustryServiceService industryServiceService) {
        this.industryServiceService = industryServiceService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(@ModelAttribute IndustryServiceSearchForm industryServiceSearchForm) {

        Page page = new Page(pageSize, industryServiceSearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/industryservice/industryServiceSearch");

        return modelAndView.addObject(industryServiceService.search(industryServiceSearchForm.getTitle(), page));
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@RequestParam("industryServiceIds") Long[] industryServiceIds) {
        industryServiceService.deleteAll(industryServiceIds);
        return "redirect:/management/industryservices/search";
    }

    @ModelAttribute("industryService")
    public IndustryService populateModel(HttpServletRequest request) {
        String strId = request.getParameter("id");

        if (strId == null || strId.equals("-1")) {
            return new IndustryService();
        }
        return industryServiceService.getById(new Long(strId));
    }

    @RequestMapping(value = "/{industryServiceId}", method = RequestMethod.GET)
    public String edit(@PathVariable Long industryServiceId, Model model) {
        IndustryService industryService = industryServiceService.getById(industryServiceId);
        model.addAttribute(industryService);
        return "management/industryservice/industryServiceForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute(new IndustryService());
        return "management/industryservice/industryServiceForm";
    }

    @ModelAttribute("allPublishStatus")
    public Map<Integer, String> populateAllPublishStatus() {
        Map<Integer, String> allPublishStatus = new HashMap<Integer, String>();
//        allPublishStatus.put(-1, "所有");
        allPublishStatus.put(0, "未发布");
        allPublishStatus.put(1, "已发布");
        return allPublishStatus;
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@ModelAttribute @Valid IndustryService industryService, BindingResult result,
                       @RequestParam("image") MultipartFile image,
                       HttpServletRequest request) throws IOException {

        if (result.hasErrors()) {
            return "management/industryservice/industryServiceForm";
        }
        industryServiceService.saveOrUpdateBean(industryService);
        if (image != null && !image.isEmpty()) {
            processImage(industryService, image, request.getSession().getServletContext());
        }

        industryServiceService.saveOrUpdateBean(industryService);

        return "redirect:/management/industryservices/search";
    }

    private void processImage(IndustryService industryService, MultipartFile image, ServletContext servletContext) throws IOException {

        String industryServicePath = servletContext.getRealPath("/upload/industryService/" + industryService.getId());
        File industryServiceDir = new File(industryServicePath);
        if (!industryServiceDir.exists()) industryServiceDir.mkdirs();

        File imageFile = new File(industryServiceDir, image.getOriginalFilename());
        if (imageFile.exists()) {
            imageFile.delete();
        }
        FileHelper.saveFile(image.getInputStream(), imageFile);
        industryService.setImageName(image.getOriginalFilename());
    }

}
